﻿using System.IO;
using Android.App;
using Android.Content.PM;
using Android.OS;
using Java.Lang;
using SQLite.Net;
using SQLite.Net.Platform.XamarinAndroid;
using Xamarin;
using Xamarin.Forms;
using XLabs.Caching;
using XLabs.Caching.SQLite;
using XLabs.Forms;
using XLabs.Ioc;
using XLabs.Platform.Device;
using XLabs.Platform.Mvvm;
using XLabs.Platform.Services;
using XLabs.Serialization;
using XLabs.Serialization.ServiceStack;

namespace SpotOffer.Droid
{
    [Activity(Label = "SpotOffer", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation/*, Theme = "@style/spotoffer"*/, Icon = "@drawable/ic_launcher")]
    public class MainActivity : XFormsApplicationDroid
    {
        /// <summary>
        /// Called when [create].
        /// </summary>
        /// <param name="bundle">The bundle.</param>
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

#if DEBUG
              Insights.Initialize(Insights.DebugModeKey, this); 
#else
            Insights.Initialize("519e5f25f548e4cfcf598a8218ca0a9475216c30", this); 
#endif

            if (!Resolver.IsSet)
            {
                this.SetIoc();
            }
            else
            {
                var app = Resolver.Resolve<IXFormsApp>() as IXFormsApp<XFormsApplicationDroid>;
                app.AppContext = this;
            }

            Forms.Init(this, bundle);
            App.Init();

            Xamarin.Forms.Forms.ViewInitialized += (sender, e) =>
            {
                if (!string.IsNullOrWhiteSpace(e.View.StyleId))
                {
                    e.NativeView.ContentDescription = e.View.StyleId;
                }
            };

            // this.SetPage(new App());
            LoadApplication(new App());
        }

        /// <summary>
        /// Sets the IoC.
        /// </summary>
        private void SetIoc()
        {
            var resolverContainer = new SimpleContainer();

            var app = new XFormsAppDroid();

            app.Init(this);

            var documents = app.AppDataDirectory;
            var pathToDatabase = Path.Combine(documents, "spotoffer.db");
            var sql = new SQLitePlatformAndroid();
            var sqlconnectionstring = new SQLiteConnectionString(pathToDatabase, true);

            resolverContainer.Register<IDevice>(t => AndroidDevice.CurrentDevice)
                  .Register<IDisplay>(t => t.Resolve<IDevice>().Display)
                //.Register<IJsonSerializer, Services.Serialization.JsonNET.JsonSerializer>()
                  .Register<IJsonSerializer, JsonSerializer>()
                  .Register<IDependencyContainer>(resolverContainer)
                  .Register<IXFormsApp>(app)
                  .Register<ISecureStorage>(t => new KeyVaultStorage(t.Resolve<IDevice>().Id.ToCharArray()))


                  .Register<ISimpleCache>(t => new SQLiteSimpleCache(sql, sqlconnectionstring, t.Resolve<IJsonSerializer>()))
                  .Register<IAsyncSimpleCache>(t => new SQLiteSimpleCache(sql, sqlconnectionstring, t.Resolve<IJsonSerializer>()));

            Resolver.SetResolver(resolverContainer.GetResolver());
        }
    }
}

